﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace FusionCharts.WebParts
{
    class ItemValueReader
    {
        /// <summary>
        /// Return the item's field value as a string
        /// Handles lookups and calculated fields and dates
        /// </summary>
        public static string GetItemValue(SPListItem item, string fieldname)
        {
            try
            {
                return GetItemValueImpl(item, fieldname);
            }
            catch
            {
                return null;
            }
        }
        private static string GetItemValueImpl(SPListItem item, string fieldname)
        {
            if (item[fieldname] == null) return null;
            string strval = item.GetFormattedValue(fieldname);
            SPField field = item.Fields.GetFieldByInternalName(fieldname);
            if (field is SPFieldUser)
            {
                // For users, we'll return their name
                strval = item[field.Id].ToString();
                SPFieldUserValue fval = new SPFieldUserValue(item.ParentList.ParentWeb, strval);
                if (fval != null && fval.User != null)
                {
                    string name = fval.User.Name;
                    return Microsoft.SharePoint.Utilities.SPEncode.HtmlEncode(name);
                }
                return null;
            }
            if (field is SPFieldLookup)
            {
                // for lookups, we'll return the string option value
                strval = item[field.Id].ToString();
                SPFieldLookup fld = (SPFieldLookup)field;
                SPFieldLookupValue fval = (SPFieldLookupValue)fld.GetFieldValue(strval);
                if (fval.LookupId == 0)
                {
                    return null;
                }
                return fval.LookupValue;
            }
            if (field.FieldValueType == Type.GetType("System.DateTime"))
            {
                // for dates, we'll return the date as a number
                // e.g., March 1, 2012 will become 2012.164
                DateTime date;
                if (!DateTime.TryParse(strval, out date)) return null;
                double numval = GetDateAsNumber(date);
                return numval.ToString();
            }
            // Not currently handling multiple values
            return strval;
        }
        /// <summary>
        /// Convert date to numerical year value
        /// E.g., March 1, 2012 => 2012.164
        /// </summary>
        private static double GetDateAsNumber(DateTime date)
        {
            System.Globalization.GregorianCalendar cal = new System.Globalization.GregorianCalendar();
            double days = cal.GetDayOfYear(date) - 1;
            days += date.Hour / 24.0;
            double value = date.Year + (days / cal.GetDaysInYear(date.Year));
            return value;
        }
    }
}
